package com.sojson.result;

import java.util.HashMap;

import com.github.pagehelper.Page;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.config.exception.TransException;
import com.sojson.result.bean.PageParam;

/**
 * 响应信息Bean
 * 
 * @author liu
 * @date 2021-09-23
 */
public class Result<T> extends HashMap<String, Object> {

    /***/
    private static final long serialVersionUID = 1L;

    /** 响应码 */
    public static final String FIELD_CODE = "code";
    /** 响应信息 */
    public static final String FIELD_MSG = "msg";
    /** 主要数据 */
    public static final String FIELD_DATA = "data";
    /** 分页数据 */
    public static final String FIELD_PAGE = "page";

    public static <T> Result<T> success() {
        TransErrorCode success = TransErrorCode.SUCCESS;
        return success(success.getCode(), success.getMsg(), null, null);
    }

    public static <T> Result<T> successMeg(String message) {
        return success(TransErrorCode.SUCCESS.getCode(), message, null, null);
    }

    public static <T> Result<T> success(String message, T data) {
        return success(TransErrorCode.SUCCESS.getCode(), message, data, null);
    }

    public static <T> Result<T> success(String message, T data, Object page) {
        return success(TransErrorCode.SUCCESS.getCode(), message, data, page);
    }

    public static <T> Result<T> success(Integer code, String message, T data, Object page) {
        Result<T> result = new Result<>();
        result.put(FIELD_CODE, code);
        result.put(FIELD_MSG, message);
        result.put(FIELD_DATA, data);
        result.put(FIELD_PAGE, page(page));
        return result;
    }

    public static <T> Result<T> success(TransException transException, T data, Object page) {
        return success(transException.getErrCode(), transException.getErrMsg(), data, page);
    }

    public static <T> Result<T> success(T data) {
        if (data instanceof Page) {
            return success(TransErrorCode.SUCCESS, data, data);
        } else {
            return success(TransErrorCode.SUCCESS, data, null);
        }
    }

    public static <T> Result<T> success(T data, Object page) {
        return success(TransErrorCode.SUCCESS, data, page);
    }

    public static <T> Result<T> success(TransErrorCode transErrorCode, T data, Object page) {
        return success(transErrorCode.getCode(), transErrorCode.getMsg(), data, page);
    }

    public static <T> Result<T> fail() {
        TransErrorCode fail = TransErrorCode.FAIL;
        return fail(fail.getCode(), fail.getMsg(), null, null);
    }

    public static <T> Result<T> fail(String message) {
        return fail(TransErrorCode.FAIL.getCode(), message, null, null);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return fail(code, message, null, null);
    }

    public static <T> Result<T> fail(Integer code, String message, T data) {
        return fail(code, message, data, null);
    }

    public static <T> Result<T> fail(Integer code, String message, T data, Object page) {
        Result<T> result = new Result<>();
        result.put(FIELD_CODE, code);
        result.put(FIELD_MSG, message);
        result.put(FIELD_DATA, data);
        result.put(FIELD_PAGE, page);
        return result;
    }

    public static <T> Result<T> fail(TransException transException) {
        return fail(transException.getErrCode(), transException.getErrMsg(), null, null);
    }

    public static <T> Result<T> fail(TransException transException, T data) {
        return fail(transException.getErrCode(), transException.getErrMsg(), data, null);
    }

    public static <T> Result<T> fail(TransException transException, T data, Object page) {
        return fail(transException.getErrCode(), transException.getErrMsg(), data, page);
    }

    public static <T> Result<T> fail(TransErrorCode transErrorCode) {
        return fail(transErrorCode.getCode(), transErrorCode.getMsg(), null, null);
    }

    public static <T> Result<T> fail(TransErrorCode transErrorCode, T data) {
        return fail(transErrorCode.getCode(), transErrorCode.getMsg(), data, null);
    }

    public static <T> Result<T> fail(TransErrorCode transErrorCode, T data, Object page) {
        return fail(transErrorCode.getCode(), transErrorCode.getMsg(), data, page);
    }

    /**
     * 解析分页插件查出来的Page对象
     * 
     * @param object
     * @return 
     * @return
     */
    public static Object page(Object object) {
        if (object instanceof Page<?>) {
            Page<?> page = (Page<?>)object;
            PageParam whPage = new PageParam();
            whPage.setPageNum(page.getPageNum() != 0 ? page.getPageNum() : 1);
            whPage.setPageSize(page.getPageSize());
            whPage.setPages(page.getPages() != 0 ? page.getPages() : 1);
            whPage.setTotal(page.getTotal());
            return whPage;
        }
        return object;
    }

    /** 获取 响应码 */
    public Integer getCode() {
        return (Integer)get(FIELD_CODE);
    }

    /** 获取 响应信息 */
    public String getMsg() {
        return (String)get(FIELD_MSG);
    }

    /** 获取 主要数据 */
    public String getData() {
        return (String)get(FIELD_DATA);
    }

    /** 设置 主要数据 */
    public void setData(T data) {
        put(FIELD_DATA, data);
    }

}